<?php
class user_model extends Model {

    public $table = 'user';

    public function is_user( $user ) {
        return $this->find( $user );
    }

    public function is_admin( $sid ) {
        $user_group = mc_model( 'sort' )->Cache()->get( 'sort' );
        $user_group = $user_group[ 'user' ];
        return $user_group[ $sid ][ 'alias' ] == 'admin';
    }

    public function is_login( $admin = true ) {
        // 检查cookie
        if ( !$cookie = $_COOKIE[ APP_COOKIE ] ) {
            return false;
        }
        $cookie = mc_md5( $cookie, 'decode' );
        $cookie = explode( '|', $cookie );

        list( $uname, $uid, $expire, $dateline ) = $cookie;

        // 检查cookie是否过期
        if ( !empty( $expire ) && $expire < time() ) {
            return false;
        }
        // 检查用户是否存在
        if ( !$result = $this->is_user( array( 'id' => $uid, 'author' => $uname, 'lastdate' => $dateline ) ) ) {
            return false;
        }
        // 检查是否有后台权限
        if ( $admin && !$this->is_admin( $result[ 'sid' ] ) ) {
            return false;
        }
        // 检查用户是否被禁用
        if ( $result[ 'status' ] ) {
            return false;
        }
        unset( $result[ 'pass' ] );
        return $result;
    }
}